package com.fava.quiz;

import java.io.IOException;
import java.security.acl.LastOwnerException;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;

public class DomandaDAO{
	
	public static void insert (Context ctx) {
		
		MyDatabase db = new MyDatabase(ctx);
		db.open();
		
		if(db.fetchDomande().getCount()==0){
			
			try {
				db.insertDomande(ctx);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    
		}
	}
	
	public static void delete (Context ctx) {
		
		MyDatabase db = new MyDatabase(ctx);
		db.open();
		
			try {
				db.deleteDomande(ctx);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    
	}
	
	public static ArrayList<Domanda> getDomande(Context ctx, Activity a) {
		ArrayList<Domanda> domande = new ArrayList<Domanda>();
		MyDatabase db = new MyDatabase(ctx);
		db.open();
		Cursor c = db.fetchDomande();
		a.startManagingCursor(c);
        int domanda=c.getColumnIndex(MyDatabase.ProductsMetaData.DOMANDA_DOMANDA_KEY);
        int r1=c.getColumnIndex(MyDatabase.ProductsMetaData.DOMANDA_R1_KEY);
        int r2=c.getColumnIndex(MyDatabase.ProductsMetaData.DOMANDA_R2_KEY); 
        int r3=c.getColumnIndex(MyDatabase.ProductsMetaData.DOMANDA_R3_KEY); 
        int r4=c.getColumnIndex(MyDatabase.ProductsMetaData.DOMANDA_R4_KEY); 
        int re=c.getColumnIndex(MyDatabase.ProductsMetaData.DOMANDA_RE_KEY);
        if(c.moveToFirst()){
                do {
                        Domanda d = new Domanda();
                        d.setDomanda(c.getString(domanda));
                        d.setR1(c.getString(r1));
                        d.setR2(c.getString(r2));
                        d.setR3(c.getString(r3));
                        d.setR4(c.getString(r4));
                        d.setrE(c.getInt(re));
                        domande.add(d);
                                        
                        } while (c.moveToNext());
        }        
        db.close();
		return domande;
	}
}
